// API 基础配置
const API_BASE_URL = '/api';

// API 服务类
class PlacesAPI {
    // 获取所有地点
    static async getAllPlaces() {
        try {
            const response = await fetch(`${API_BASE_URL}/places.php`);
            if (!response.ok) throw new Error('获取地点列表失败');
            return await response.json();
        } catch (error) {
            console.error('获取地点列表错误:', error);
            throw error;
        }
    }

    // 获取单个地点详情
    static async getPlace(id) {
        try {
            const response = await fetch(`${API_BASE_URL}/places.php?id=${id}`);
            if (!response.ok) throw new Error('获取地点详情失败');
            return await response.json();
        } catch (error) {
            console.error('获取地点详情错误:', error);
            throw error;
        }
    }

    // 添加新地点
    static async addPlace(placeData) {
        try {
            const response = await fetch(`${API_BASE_URL}/places.php`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(placeData)
            });
            if (!response.ok) throw new Error('添加地点失败');
            return await response.json();
        } catch (error) {
            console.error('添加地点错误:', error);
            throw error;
        }
    }

    // 更新地点信息
    static async updatePlace(id, placeData) {
        try {
            const response = await fetch(`${API_BASE_URL}/places.php?id=${id}`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(placeData)
            });
            if (!response.ok) throw new Error('更新地点失败');
            return await response.json();
        } catch (error) {
            console.error('更新地点错误:', error);
            throw error;
        }
    }

    // 删除地点
    static async deletePlace(id) {
        try {
            const response = await fetch(`${API_BASE_URL}/places.php?id=${id}`, {
                method: 'DELETE'
            });
            if (!response.ok) throw new Error('删除地点失败');
            return await response.json();
        } catch (error) {
            console.error('删除地点错误:', error);
            throw error;
        }
    }

    // 搜索地点
    static async searchPlaces(keyword, options = {}) {
        try {
            const queryParams = new URLSearchParams({
                keyword,
                ...options
            });
            const response = await fetch(`${API_BASE_URL}/places.php?${queryParams}`);
            if (!response.ok) throw new Error('搜索地点失败');
            return await response.json();
        } catch (error) {
            console.error('搜索地点错误:', error);
            throw error;
        }
    }

    // 批量添加地点
    static async batchAddPlaces(placesData) {
        try {
            const response = await fetch(`${API_BASE_URL}/places.php`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-Batch-Operation': 'true'
                },
                body: JSON.stringify(placesData)
            });
            if (!response.ok) throw new Error('批量添加地点失败');
            return await response.json();
        } catch (error) {
            console.error('批量添加地点错误:', error);
            throw error;
        }
    }

    // 导出地点数据
    static async exportPlaces(format = 'json') {
        try {
            const response = await fetch(`${API_BASE_URL}/places.php?export=true&format=${format}`);
            if (!response.ok) throw new Error('导出数据失败');
            return await response.blob();
        } catch (error) {
            console.error('导出数据错误:', error);
            throw error;
        }
    }

    // 导入地点数据
    static async importPlaces(file) {
        try {
            const formData = new FormData();
            formData.append('file', file);

            const response = await fetch(`${API_BASE_URL}/places.php?import=true`, {
                method: 'POST',
                body: formData
            });
            if (!response.ok) throw new Error('导入数据失败');
            return await response.json();
        } catch (error) {
            console.error('导入数据错误:', error);
            throw error;
        }
    }
}

// 地图工具类
class MapUtils {
    // 坐标转换：WGS84 转 GCJ02
    static wgs84ToGcj02(lng, lat) {
        // 实现坐标转换逻辑
    }

    // 坐标转换：GCJ02 转 WGS84
    static gcj02ToWgs84(lng, lat) {
        // 实现坐标转换逻辑
    }

    // 计算两点之间的距离
    static calculateDistance(point1, point2) {
        // 实现距离计算逻辑
    }

    // 计算多边形面积
    static calculateArea(points) {
        // 实现面积计算逻辑
    }
}

// 导出
window.PlacesAPI = PlacesAPI;
window.MapUtils = MapUtils; 